package com.weijian.controller.admin;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijian.common.pojo.CommonResult;
import com.weijian.common.pojo.PageResponse;
import com.weijian.dal.entity.Category;
import com.weijian.dal.query.CategoryPageQuery;
import com.weijian.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
@RequiredArgsConstructor
public class CategoryController {

    final CategoryService categoryService;

    @Operation(summary = "分页查询分类")
    @PostMapping("/list")
    public PageResponse<Category> queryCategoryByPage(CategoryPageQuery query) {
        Page<Category> categoryPage = categoryService.lambdaQuery()
                .like(StrUtil.isNotBlank(query.getKey()), Category::getName, query.getKey())
                .eq(Category::getStatus, 1)
                .page(query.toMpPage("update_time", false));
        return PageResponse.of(categoryPage, Category.class);
    }

    @Operation(summary = "根据id批量查询菜品")
    @GetMapping("/gets")
    public CommonResult<List<Category>> queryCategoryByIds(@RequestParam("ids") List<Long> ids) {
        return categoryService.queryCategoryByIds(ids);
    }

    @Operation(summary = "根据id查询菜品")
    @GetMapping("/get")
    public CommonResult<Category> queryCategoryById(@RequestParam("id") Long id) {
        return categoryService.queryCategoryById(id);
    }

    @Operation(summary = "新增分类")
    @PostMapping("/create")
    public void saveCategory(@RequestBody Category category) {
        categoryService.save(category);
    }

    @Operation(summary = "更新分类状态")
    @PutMapping("/status/{id}/{status}")
    public void updateCategoryStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        categoryService.updateById(category);
    }

    @Operation(summary = "更新分类")
    @PutMapping("/update")
    public void updateCategory(@RequestBody Category category) {
        // 不允许修改菜品状态，所以强制设置为null，更新时，就会忽略该字段
        category.setStatus(null);
        // 更新
        categoryService.updateCategory(category);
    }

    @Operation(summary = "根据id删除菜品")
    @DeleteMapping("/delete")
    public void deleteCategoryById(@RequestParam("id") Long id) {
        categoryService.deleteCategoryById(id);
    }

    @Operation(summary = "批量删除分类")
    @DeleteMapping("/deletes")
    public void deleteCategoryByIds(@RequestParam("ids") List<Long> ids) {
        categoryService.deleteCategoryByIds(ids);
    }

    @Operation(summary = "根据类型id获取分类集合")
    @GetMapping("/categoryId")
    public CommonResult<List<Category>> listByTypeId(@RequestParam("categoryId") Long categoryId) {
        return categoryService.listByTypeId(categoryId);
    }
}
